﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Specialized;
using SyCODE.Component.Security;
using SyCODE.Component.DataFactory;
using SyCODE.Component;
using System.Xml;
using System.IO;
using System.Data.Common;

namespace SyCODE.Blog
{
    /// <summary>
	/// GetTopicIndex 的摘要说明
    /// </summary>
	public class GetTopicIndex : IHttpHandler
    {
        #region IHttpHandler 成员

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            NameValueCollection nvc = new NameValueCollection(context.Request.QueryString);

			string cls = nvc["c"];

			if (String.IsNullOrEmpty(cls))
			{
				nvc.Add("c", cls = "0");
			}
			if (String.IsNullOrEmpty(nvc["p"]))
			{
				nvc.Add("p", "1");
			}
			if (String.IsNullOrEmpty(nvc["s"]))
			{
				nvc.Add("s", "25");
			}

            DBAM dbam = new DBAM();
            try
            {
                dbam.BuildParameters("topicindex", nvc);

                context.Response.Expires = 120;
                context.Response.ContentType = "text/xml";

                XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, context.Response.ContentEncoding);

                writer.WriteStartDocument(); 
                writer.WriteProcessingInstruction("xml-stylesheet", "type='text/xsl' href='/ui/common/index.xsl'");
                
                writer.WriteStartElement("html");
                writer.WriteAttributeString("c", nvc["c"]);
                writer.WriteAttributeString("p", nvc["p"]);
                writer.WriteAttributeString("s", nvc["s"]);

                writer.WriteStartElement("head");

                writer.WriteStartElement("meta");
                writer.WriteAttributeString("http-equiv", "Content-Type");
                writer.WriteAttributeString("content", "text/html; charset=" + context.Response.ContentEncoding.WebName);
                writer.WriteEndElement();

                writer.WriteStartElement("title");
				writer.WriteString(StringResource.GetField("sitetopicindex"));

				if (cls != "0")
				{
					if (nvc["c"].Length > 2)
					{
						string mcls = cls.Substring(0, cls.Length - 2);

						writer.WriteString(" - ");
						writer.WriteString(ConstData.Get("category", mcls).Value);
						writer.WriteString(" - ");
						writer.WriteString(ConstData.Get("category", new string[] { mcls, cls }).Value);
					}
					else
					{
						writer.WriteString(" - ");
						writer.WriteString(ConstData.Get("category", cls).Value);
					}
				}

				writer.WriteString(" - Powered By X3BLOG");
                writer.WriteEndElement();

                writer.WriteEndElement();

                writer.WriteStartElement("body");

                DbDataReader dr = dbam.ExecuteReader();

                while (dr.Read())
                {
					string upath = "/" + dr.GetString(3);
                    writer.WriteStartElement("p");

					writer.WriteStartElement("a");
					writer.WriteAttributeString("href", upath + "/topic/" + dr.GetInt64(0).ToString());
					writer.WriteString(dr.GetString(1));
					writer.WriteEndElement();

                    writer.WriteStartElement("a");
					writer.WriteAttributeString("href", upath);
                    writer.WriteString(dr.GetString(4));
                    writer.WriteEndElement();

                    writer.WriteStartElement("span");
                    writer.WriteString(dr.GetDateTime(2).ToString("yyyy-MM-dd"));
                    writer.WriteEndElement();

					int view = dr.GetInt32(5);
					writer.WriteStartElement("span");
					writer.WriteString(view > 999999 ? (view / 10000).ToString() : view.ToString());
					writer.WriteEndElement();

					int comment = dr.GetInt32(6);
					writer.WriteStartElement("span");
					writer.WriteString(view > 999999 ? (comment / 10000).ToString() : comment.ToString());
					writer.WriteEndElement();

                    writer.WriteEndElement();
                }

                dr.NextResult();
                dr.Read();

				int page = Int32.Parse(nvc["p"]);
				int count = dr.GetInt32(0);
                writer.WriteStartElement("span");
				writer.WriteString(count.ToString());
                writer.WriteEndElement();

				if (Math.Ceiling((float)count / 25f) > page)
				{
					writer.WriteStartElement("a");
					writer.WriteAttributeString("href", "index.aspx?c=" + nvc["c"] + "&p=" + (page + 1).ToString());
					writer.WriteString("[next page]");
					writer.WriteEndElement();
				}

                dr.Close();

                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
            }
            finally
            {
                dbam.Dispose();
            }
        }

        #endregion
    }
}